<template>
  <!--按钮-->
  <div>
    <el-button type="danger" :icon="Plus" @click="PigsBreedings">
      新增
    </el-button>
    <el-button type="danger" :icon="DocumentAdd" disabled>保存</el-button>
    <el-button type="danger" :icon="BottomRight" disabled>撤销</el-button>
    <el-button type="danger" :icon="Delete" disabled>删除</el-button>
  </div>

  <div style="margin-top: 20px; margin-bottom: 20px">
    <span>
      负责人：
      {{ form[0].leader }}&ensp;
    </span>
    <span style="margin-left: 100px">
      配种日期：
      {{ form[0].eventDate }}&ensp;
    </span>
    <span style="margin-left: 100px">单据号&ensp;-- --&ensp;</span>
    <span style="margin-left: 100px">流水号&ensp;-- --&ensp;</span>
  </div>
  <hr size="6" style="background-color: rgb(232.8, 233.4, 234.6)" />
  <!--表格-->
  <table>
    <tr>
      <td width="150">序号</td>
      <td width="150">批次/耳号</td>
      <td width="150">栋舍</td>
      <td width="150">转入栏位</td>
      <td width="150">性别</td>
      <td width="150">商品名称</td>
      <td width="150">标包</td>
      <td width="150">计算单位</td>
      <td width="150">入库数量</td>
      <td width="150">只均单价(元)</td>
      <td width="150">金额(元)</td>
      <td width="150">单位折扣(元)</td>
      <td width="150">调整金额(元)</td>
      <td width="150">结算金额(元)</td>
      <td width="150">总重量(公斤)</td>
    </tr>

    <tr v-for="(item, index) in form" :key="index">
      <td width="180"><el-input :value="index + 1" /></td>
      <td width="180">
        <el-select
          v-model="item.basicInforId"
          placeholder="请选择"
          @change="handlebedroomField(item.basicInforId, index)"
          clearable
        >
          <el-option
            v-for="basic in basicInforList"
            :key="basic.basicInforId"
            :label="basic.earNumber"
            :value="basic.basicInforId"
          />
        </el-select>
      </td>
      <td width="180"><el-input v-model="item.breedingWeight" /></td>
      <td width="180"><el-input v-model="item.breedingWeight" /></td>
      <td width="180"><el-input v-model="item.breedingWeight" /></td>
      <td width="180"><el-input v-model="item.firstmatchrating" /></td>
      <td width="180"><el-input v-model="item.breedingWeight" /></td>
      <td width="180"><el-input v-model="item.breedingWeight" /></td>
      <td width="180"><el-input v-model="item.breedingWeight" /></td>
      <td width="180"><el-input v-model="item.breedingWeight" /></td>
      <td width="180"><el-input v-model="item.breedingWeight" /></td>
      <td width="180"><el-input v-model="item.breedingWeight" /></td>
      <td width="180"><el-input v-model="item.breedingWeight" /></td>
      <td width="180"><el-input v-model="item.breedingWeight" /></td>
      <td width="180"><el-input v-model="item.breedingWeight" /></td>
    </tr>
  </table>
  <!--增行 删行-->
  <div>
    <el-button type="danger" link @click="addRow">+增行</el-button>
    <el-button
      type="danger"
      link
      @click="removeRow"
      :disabled="form.length <= 0"
      >-删行</el-button
    >
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from "vue";
import { ElMessage } from "element-plus";
import { type FormInstance, type FormRules } from "element-plus";
import axios from "axios";
import { useRouter, useRoute } from "vue-router";
import {
  Plus,
  Delete,
  DocumentAdd,
  BottomRight,
} from "@element-plus/icons-vue";

const apiUrl = import.meta.env.VITE_APP_API_URL;
const apiBedinUrl = import.meta.env.VITE_APPRead_API_URL;

const router = useRouter();
const route = useRoute();

//品种
const varieties = ref([]);
const getVarieties = () => {
  axios
    .post(apiBedinUrl+"/api/Basics/SearchVarieties", {})
    .then((res) => {
      console.log("品种", res.data.data);
      varieties.value = res.data.data;
    });
};

const size = ref<"default" | "large" | "small">("default");

//当前时间
const date = new Date();

//响应式数据 获取栋舍
const condition = reactive({
  pageIndex: 1,
  pageSize: 100,
  bedroomTypeId: 0,
  bedroomName: "",
  personName: "",
});

const form = ref<any>([
  {
    pigsBreedingId: 0,
    leader: localStorage.getItem("userName"),
    lastBreedingDate: new Date(),
    basicInforId: "",
    fatherEarNumber: "",
    breedingmethodId: 1,
    estrustype: 1,
    firstmatchrating: 0,
    currentbuilding: "",
    currentfield: "",
    transferbuilding: 0,
    transferfield: 0,
    reproductivestatus: 1,
    eventDate: "",
    documentNo: "",
    serialNo: "",
    breedingWeight: 0,
    isDeleted: 0,
    createdOwner: localStorage.getItem("userName"),
    createdTime: new Date(),
    updateOwner: "",
    updatedTime: null,
    examineOwner: "",
    examineTime: null,
  },
]);

//增行 删行
const addRow = () => {
  form.value.push({
    pigsBreedingId: 0,
    leader: localStorage.getItem("userName"),
    lastBreedingDate: new Date(),
    basicInforId: "",
    fatherEarNumber: "",
    breedingmethodId: 1,
    estrustype: 1,
    firstmatchrating: 0,
    currentbuilding: "",
    currentfield: "",
    transferbuilding: 0,
    transferfield: 0,
    reproductivestatus: 1,
    eventDate: "",
    documentNo: "",
    serialNo: "",
    breedingWeight: 0,
    isDeleted: 0,
    createdOwner: localStorage.getItem("userName"),
    createdTime: new Date(),
    updateOwner: "",
    updatedTime: null,
    examineOwner: "",
    examineTime: null,
  });
  console.log(form.value);
};
const removeRow = () => {
  if (form.value.length > 0) {
    form.value.pop();
  }
};

//新增
const PigsBreedings = () => {
  console.log(form.value);
  const obj = {
    pigsBreedings: form.value,
  };
  axios
    .post(apiBedinUrl+"/api/PigBreedings/AddPigsBreeding", obj)
    .then((res) => {
      if (res.data.code == 0) {
        ElMessage.success("添加猪只采购成功");
      } else {
        ElMessage.error("添加猪只采购失败");
      }
    });
};

//获取耳号
const basicInforList: any = ref([]);
const handlebasicInfor = () => {
  axios.post(apiBedinUrl+"/api/Basics/SearchBasic", {}).then((res) => {
    basicInforList.value = res.data.data;
  });
};

//获取当前栋舍 栏位
const bedroomField: any = reactive({
  currentbuilding: "",
  currentfield: "",
});
//将该方法放在耳号下拉框 变化的时候将id值与下标传过来了
const handlebedroomField = (id: number, index: number) => {
  axios
    .post(apiBedinUrl+"/api/Basics/SearchBedroomField", {
      //根据耳号id进行获取
      basicInforId: id,
    })
    .then((res) => {
      console.log(res.data.data);
      //给栋舍 栏位 性别赋值
      bedroomField.currentbuilding = res.data.data.bedroomName;
      bedroomField.currentfield = res.data.data.fieldName;
      console.log(bedroomField);
      //根据下标进行赋值
      form.value[index].currentbuilding = bedroomField.currentbuilding;
      form.value[index].currentfield = bedroomField.currentfield;
      console.log(form);
    });
};

//获取栋舍
const bedroomList: any = ref([]);
const handleBedroom = () => {
  axios
    .post(apiUrl+"/api/BedRoom/ShowBedroom", {
      pageIndex: condition.pageIndex,
      pageSize: condition.pageSize,
      bedroomTypeId: condition.bedroomTypeId,
      bedroomName: condition.bedroomName,
      personName: condition.personName,
    })
    .then((res) => {
      bedroomList.value = res.data.data.list;
    });
};

//获取栏位
const fieldList: any = ref([]);
const handlefield = (id: number, index: number) => {
  form.value[index].fieldId = "";
  axios
    .post(apiUrl+"/api/BedRoom/GetBrdroomIdField", {
      bedroomId: id,
    })
    .then((res) => {
      fieldList.value[index] = res.data.data;
    });
};
//生命周期钩子
onMounted(() => {
  handlebasicInfor();
  handleBedroom();
  getVarieties();
});
</script>

<style scoped></style>
